package com.ynkbny.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.base.PageResult;
import com.ynkbny.domain.Enterprise;
import com.ynkbny.domain.Token;
import com.ynkbny.domain.User;
import com.ynkbny.mapper.TokenMapper;
import com.ynkbny.mapper.UserMapper;
import com.ynkbny.service.TokenService;
import com.ynkbny.service.UserService;
import com.ynkbny.util.SaltUtil;
import com.ynkbny.util.SearchUtil;
import com.ynkbny.util.UserRoleUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

/**
 * @author zelin.wang
 * @description 用户实现类
 * @date 8/14/2023
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserRoleUtil<User> userRoleUtil;

    @Override
    public boolean addUser(User user) {
        //1、数据库查找用户名为username的数据
        String username=user.getUserName();
        User result=this.getOne(new QueryWrapper<User>().eq("user_name",username));
        //2、获取当前登录用户信息
        Subject subject = SecurityUtils.getSubject();
        String userName = (String) subject.getPrincipal();
        User user1 = this.baseMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, userName));
        //3、判断数据是否存在
        //不存在则允许添加用户
        if (result==null) {
            //1.获取随机盐
            String salt = SaltUtil.getSalt(8);
            //2.将随机盐保存到数据
            user.setSalt(salt);
            //3.明文密码进行md5 + salt + hash散列
            Md5Hash MD5 = new Md5Hash(user.getPassword(), salt, 1024);
            user.setPassword(MD5.toHex());
            //4.判断登录用户是否为企业管理员 将新增用户的up_id赋值为企业管理员id
            if (user1.getRoleId().equals("4")){
                user.setUpId(user1.getId());
            }
            return this.saveOrUpdate(user);
        }else {
            //存在则返回错误信息
            return false;
        }
    }

    @Override
    public boolean deleteUser(User user) {
        return this.removeById(user);
    }

    @Override
    public boolean editUser(User user) {
        //查询ID不同但是账号相同的数据，说明修改账号有重复
        User result=getOne(new QueryWrapper<User>().eq("user_name",user.getUserName()).ne("id",user.getId()));
        User user1 = this.getById(user.getId());
        if (result==null){
            user1.setUserName(user.getUserName());
            user1.setNickName(user.getNickName());
            user1.setRoleId(user.getRoleId());
            return  saveOrUpdate(user1);
        }else {
            return false;
        }
    }

    @Override
    public PageResult<User> pageList(String condition, int pageNum, int pageSize) {
        Page<User> page=new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (condition!=null){
            queryWrapper.like("user_name",condition);
        }
        Page<User> pageList = userRoleUtil.getUserPageList(page, queryWrapper, this.baseMapper);
        return new PageResult<>(pageList);
    }
    @Override
    public boolean saveList(List<User> users){
        return this.saveBatch(users);
    }
}
